package com.supor.aiot.home.helper;

import android.text.TextUtils;

import com.android.baseconfig.base.BaseSharedPreferences;
import com.android.baseconfig.common.utils.GsonUtil;
import com.android.baseconfig.common.utils.Logc;
import com.android.baseconfig.common.utils.ThreadUtils;
import com.google.gson.reflect.TypeToken;
import com.supor.aiot.common.app.AppAgent;
import com.supor.aiot.common.app.Constants;
import com.supor.aiot.common.helper.WifiStationManager;
import com.supor.nfc.lib.common.NfcRecipeParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: DataCache <br>
 * 作者: lei <br>
 * 日期: 2020/12/4 <br>
 */
public class DataCache {

    /**
     * 当前配网WiFi SSID
     */
    public static String CURRENT_SSID;
    /**
     * 当前配网WiFi密码
     */
    public static String CURRENT_PWD;
    /**
     * 机智云IoT平台域名解析IP
     */
    public static String GIZ_IP;
    public static boolean NEED_CLOSE_ACCOUNT = false;
    /**
     * <p>配网计时</p>
     * AP：从发送密码到主厨机开始计时<br>
     * NFC：从碰一碰配网开始计时<br>
     */
    public static long CONFIG_START_TIME = 0;

    public static void cacheWifi(String ssid, String pwd) {
        String wifiJson = BaseSharedPreferences.getString(AppAgent.getApp(),
                Constants.Key.WIFI_SAVE_JSON);
        Map<String, String> wifiMap;
        if (!TextUtils.isEmpty(wifiJson)) {
            wifiMap = GsonUtil.getInstance().toObject(wifiJson,
                    new TypeToken<Map<String, String>>() {
                    }.getType());
        } else {
            wifiMap = new HashMap<>();
        }
        wifiMap.put(ssid, pwd);
        BaseSharedPreferences.setString(AppAgent.getApp(), Constants.Key.WIFI_SAVE_JSON,
                GsonUtil.getInstance().toJson(wifiMap));
    }

    public static void removeWifi(String ssid) {
        String wifiJson = BaseSharedPreferences.getString(AppAgent.getApp(),
                Constants.Key.WIFI_SAVE_JSON);
        Map<String, String> wifiMap;
        if (!TextUtils.isEmpty(wifiJson)) {
            wifiMap = GsonUtil.getInstance().toObject(wifiJson,
                    new TypeToken<Map<String, String>>() {
                    }.getType());
        } else {
            wifiMap = new HashMap<>();
        }
        wifiMap.remove(ssid);
        BaseSharedPreferences.setString(AppAgent.getApp(), Constants.Key.WIFI_SAVE_JSON,
                GsonUtil.getInstance().toJson(wifiMap));
    }

    public static String getWifiPwd(String ssid) {
        String wifiJson = BaseSharedPreferences.getString(AppAgent.getApp(),
                Constants.Key.WIFI_SAVE_JSON);
        if (!TextUtils.isEmpty(wifiJson)) {
            Map<String, String> wifiMap = GsonUtil.getInstance().toObject(wifiJson,
                    new TypeToken<Map<String, String>>() {
                    }.getType());
            return wifiMap.get(ssid);
        } else {
            return null;
        }
    }

    public static List<NfcRecipeParam> getCacheRecipeBuilds() {
        String json = BaseSharedPreferences.getString(AppAgent.getApp(),
                Constants.Key.My_RECIPE_BUILD_JSON);
        List<NfcRecipeParam> list;
        if (!TextUtils.isEmpty(json)) {
            list = GsonUtil.getInstance().toObject(json,
                    new TypeToken<List<NfcRecipeParam>>() {
                    }.getType());
        } else {
            list = new ArrayList<>();
        }
        return list;
    }

    public static List<NfcRecipeParam> getCacheRecipeBuilds(String deviceType) {
        String json = BaseSharedPreferences.getString(AppAgent.getApp(),
                Constants.Key.My_RECIPE_BUILD_JSON);
        List<NfcRecipeParam> list;
        if (!TextUtils.isEmpty(json)) {
            list = GsonUtil.getInstance().toObject(json,
                    new TypeToken<List<NfcRecipeParam>>() {
                    }.getType());
            List<NfcRecipeParam> typeList = new ArrayList<>();
            for (NfcRecipeParam recipeParam : list) {
                if (recipeParam.deviceType.equalsIgnoreCase(deviceType)) {
                    typeList.add(recipeParam);
                }
            }
            return typeList;
        } else {
            list = new ArrayList<>();
        }
        return list;
    }

    /**
     * 获取机智云域名对应的IP地址
     */
    public static void getGIZIp() {
        ThreadUtils.execute(() -> {
            if (AppAgent.currentServer == 2) {
                DataCache.GIZ_IP = WifiStationManager.getInetAddress(Constants.GIZ_TEST_HOST);
            } else {
                DataCache.GIZ_IP = WifiStationManager.getInetAddress(Constants.GIZ_RELEASE_HOST);
            }
            Logc.d("giz ip: " + DataCache.GIZ_IP);
            if (!TextUtils.isEmpty(GIZ_IP)) {
                BaseSharedPreferences.setString(AppAgent.getApp(),
                        Constants.Key.GIZ_IP, GIZ_IP);
            }
        });
    }
}
